# coding=utf-8

import logging
from logging.handlers import RotatingFileHandler


def log_log(name=None,
            logfile=None,
            level=logging.DEBUG,
            formatter=None,
            maxBytes=0,
            backupCount=0,
            fileLoglevel=None,
            console = False,
            encoding=None,
            delay=0):
	"""
	返回一个logger对象
	:param name: Logger对象名称
	:param logfile: 日志文件名称
	:param level: Logger对象等级
	:param formatter: 日志输出格式
	:param maxBytes: 单个日志文件最大容量，单位byte
	:param backupCount: 保存的日志文件个数
	:param fileLoglevel: 日志文件的日志等级
	:param console: 是否输出到控制台
	:param encoding: 编码方式
	:param delay: 延时时间
	:return: Logger对象
	"""

	_logger = logging.getLogger(name or __name__)
	_logger.setLevel(level = level)

	#定义一个RotatingFileHandler，最多备份3个日志文件，每个日志文件最大1K
	_handler = RotatingFileHandler(filename=logfile,
	                               mode='a',
	                               maxBytes=maxBytes,
	                               backupCount=backupCount,
	                               encoding=encoding,
	                               delay=delay)

	_handler.setLevel(fileLoglevel or level)
	if not formatter:
		formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

	_handler.setFormatter(formatter)
	_logger.addHandler(_handler)

	if console:
		console = logging.StreamHandler()
		console.setLevel(fileLoglevel or level)
		console.setFormatter(formatter)
		logger.addHandler(console)

	return _logger

if __name__ == "__main__":
	logger = log_log(name='log', logfile='log.log', maxBytes=1024*100, backupCount=1)
	# for i in range(3000):
	# 	logger.debug("hahahhahhaha--------------------"+str(i))
	logger.info('-----update coil_stock_class -------')